<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="郑浩枫,周芳怡,咸鱼郑某的博客" />
   
  <meta name="description" content="咸鱼郑某的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java学习 |  岛
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/atom.xml" title="岛" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Java学习-01" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java学习
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2021/Java%E5%AD%A6%E4%B9%A0/" class="article-date">
  <time datetime="2021-01-21T06:31:11.000Z" itemprop="datePublished">2021-01-21</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%8D%9A%E5%AE%A2/">博客</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">7.6k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">32分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      
      

      
      <h1 id="JAVA笔记"><a href="#JAVA笔记" class="headerlink" title="JAVA笔记"></a>JAVA笔记</h1><span id="more"></span>

<h1 id="JDK的安装"><a href="#JDK的安装" class="headerlink" title="JDK的安装"></a>JDK的安装</h1><ol>
<li>下载安装JDK</li>
<li>配置JDK环境变量</li>
</ol>
<h3 id="下载安装JDK"><a href="#下载安装JDK" class="headerlink" title="下载安装JDK"></a>下载安装JDK</h3><p>JRE 是Java的运行环境<br>JDK 是Java的开发环境</p>
<p>Java SE - 基础核心<br>Java ME - 移动设备/游戏/通信<br>Java EE - 网站开发</p>
<ol>
<li><p>安装JDK ，找到安装路径 ：C:\Program Files\Java\jdk-12.0.2</p>
</li>
<li><p>复制，在设置中找到高级设置，系统变量新建JAVA_HOME变量，变量值的刚刚的路径</p>
</li>
<li><p>找到Path 变量，末尾加分号（WIN10不用），添加%JAVA_HOME%\bin进去</p>
</li>
</ol>
<h4 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h4><p>打开CMD，输入 javac，有返回值则配置完成</p>
<h3 id="输出乱码"><a href="#输出乱码" class="headerlink" title="输出乱码"></a>输出乱码</h3><p>编译的时候指定编码：</p>
<blockquote>
<p>javac -encoding utf-8 *.java</p>
</blockquote>
<h3 id="java格式"><a href="#java格式" class="headerlink" title="java格式"></a>java格式</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs plain">public class test1 &#123;<br>	public static void main(String[] args) &#123;<br>		System.out.println(&quot;Hello Word!&quot;);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><ol>
<li>单行</li>
</ol>
<blockquote>
<p>//</p>
</blockquote>
<ol start="2">
<li><p>多行</p>
<blockquote>
<p>/*<br>····<br>*/</p>
</blockquote>
</li>
<li><p>文档注释</p>
<blockquote>
<p>/** </p>
<p>*/</p>
</blockquote>
</li>
</ol>
<h3 id="Java项目导入导出"><a href="#Java项目导入导出" class="headerlink" title="Java项目导入导出"></a>Java项目导入导出</h3><ol>
<li>大小可变的空间在堆中开辟（String），大小固定的在栈区中开辟（int），在栈中保存内存地址</li>
<li>栈的存取速度比堆快</li>
</ol>
<h3 id="占用字节"><a href="#占用字节" class="headerlink" title="占用字节"></a>占用字节</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs plain">byte :1<br>short :2<br>int:4<br>long:8<br>float:4<br>double:8<br>char:1<br>boolead:1<br></code></pre></td></tr></table></figure>

<h3 id="强制转换"><a href="#强制转换" class="headerlink" title="强制转换"></a>强制转换</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">int a &#x3D; 10;<br>byte b &#x3D; (byte)a;<br></code></pre></td></tr></table></figure>
<h3 id="a-与-a"><a href="#a-与-a" class="headerlink" title="++a 与 a++"></a>++a 与 a++</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs plain">c &#x3D; ++a + b <br>&#x2F;&#x2F;先算++a ,返回结果再+b<br>c &#x3D; a++ b<br>&#x2F;&#x2F;先算a + b 给c ,然后a++<br><br></code></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">double c &#x3D; 10.0 &#x2F; 3;<br>System.out.print(c);<br>&#x2F;&#x2F;如果10不是10.0，则输出整数<br></code></pre></td></tr></table></figure>

<h3 id="接收用户键盘输入"><a href="#接收用户键盘输入" class="headerlink" title="接收用户键盘输入"></a>接收用户键盘输入</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs plain">import Java.util.Scnner;<br>Scnneer input &#x3D; Scnner(System.in); &#x2F;&#x2F;获取Scnner对象，用来支持接受用户输入的功能<br>String x &#x3D; input.next();&#x2F;&#x2F;输入字符串<br>int x &#x3D; input.nextInt();&#x2F;&#x2F;输入数值<br>System.out.println(x)<br></code></pre></td></tr></table></figure>
<h3 id="判断字符相等"><a href="#判断字符相等" class="headerlink" title="判断字符相等"></a>判断字符相等</h3><p>用== 判读的是内存地址是否相等，用str.equals(str)来判断</p>
<h3 id="switch-case"><a href="#switch-case" class="headerlink" title="switch-case"></a>switch-case</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs plain">switch(x)&#123;<br>	case 1:<br>		······<br>		break;<br>	case 2:<br>		······<br>		break;<br>	default:<br>		······<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="常用函数"><a href="#常用函数" class="headerlink" title="常用函数"></a>常用函数</h3><ul>
<li>Math.ceil //向上取整</li>
<li>Math.floor //向下取整</li>
<li>Math.random //  0.-0.9随机整数</li>
<li>str.split() //拆分字符串</li>
<li>str.charAt(“位置”) //获取单个字符</li>
<li>str.substring(num,num); //截取子字符串</li>
<li>str.indexOf(); //获取指定字符串的位置</li>
<li>String.valueOf(); //转换成字符串<h3 id="随机数"><a href="#随机数" class="headerlink" title="随机数"></a>随机数</h3><blockquote>
<p>Math.random();<br>//或<br>Random e = new Random();<br>int ra2=e.nextInt(X);<br>System.out.println(ra2);</p>
</blockquote>
</li>
</ul>
<h3 id="修饰符"><a href="#修饰符" class="headerlink" title="修饰符"></a>修饰符</h3><ul>
<li><strong>default</strong>  (即默认，什么也不写）: 在同一包内可见，不使用任何修饰符。使用对象：类、接口、变量、方法。</li>
<li><strong>private</strong>  : 在同一类内可见。使用对象：变量、方法。  <strong>注意：不能修饰类（外部类）</strong></li>
<li><strong>public</strong>  : 对所有类可见。使用对象：类、接口、变量、方法</li>
<li>  <strong>protected</strong>  : 对同一包内的类和所有子类可见。使用对象：变量、方法。  <strong>注意：不能修饰类（外部类）</strong>。</li>
</ul>
<h3 id="break-和-continue"><a href="#break-和-continue" class="headerlink" title="break 和 continue"></a>break 和 continue</h3><p>break跳出循环<br>continue跳出本次循环，执行下一次循环</p>
<h3 id="重载"><a href="#重载" class="headerlink" title="重载"></a>重载</h3><p>当定义的功能相同，但参与运算的内容不同，这时就定义一个函数名称以表示其功能方便阅读。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs plain">add(int a,int b)&#123;<br>···<br>&#125;<br>add(int a,int b,int c)&#123;<br>···<br>&#125;<br></code></pre></td></tr></table></figure>


<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">int x &#x3D; new int[num]; &#x2F; int[] x &#x3D; new int[]; &#x2F; int x[] &#x3D; new int[];<br>或<br>int x &#x3D; new int[]&#123;x1,x2,x3··&#125;<br></code></pre></td></tr></table></figure>
<p>数组有单独是数据类型，数组类型。</p>
<p>局部变量在栈内存中。<br>new 操作出来的变量都在堆内存中。</p>
<p>数组 x 在栈内存中存放数组地址值，在堆内存中存放数组</p>
<p>定义好数组后都有默认值，int 为0，等</p>
<h3 id="面向对象思想"><a href="#面向对象思想" class="headerlink" title="面向对象思想"></a>面向对象思想</h3><p>三个特征：继承，封装，多态<br>开发：找对象使用，没有对象就创建对象<br>找对象，建立对象，使用对象，维护对象的关系<br>具体对象就是java    在堆内存中用new    建立的实体</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs plain">class x&#123;<br>	void run()&#123;<br>		···<br>	&#125;<br>	·····<br>&#125;<br><br>public static void main()&#123;<br>	x a &#x3D; new x(); &#x2F;&#x2F;类类型变量<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>成员变量和局部变量：<br>成员变量作用于整个类中，局部变量作用于函数中，    或者语句中。<br>成员变量在堆内存中，局部变量在栈内存中。</p>
<p>匿名对象使用：当对对象的方法只调用一次时，可以使用匿名对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plain">new x().run()<br></code></pre></td></tr></table></figure>
<p>如果对一个对象进行多个成员的调用，必须给这个对象起个名字。<br>可以将匿名对象作为实际参数进行传递。</p>
<h3 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h3><p>是指隐藏对象的属性和实现细节，仅对外提供公共的访问方式。</p>
<p>好处：</p>
<ul>
<li>将变化隔离</li>
<li>便于使用</li>
<li>提高重用性</li>
<li>提高安全性</li>
</ul>
<h4 id="封装原则"><a href="#封装原则" class="headerlink" title="封装原则"></a>封装原则</h4><ul>
<li>将不需要对位提供的内容都隐藏起来。</li>
<li>把属性都隐藏，提供公共方法对其访问。 </li>
</ul>
<h3 id="this"><a href="#this" class="headerlink" title="this"></a>this</h3><p>this 代表本类对象。代表它所在函数所属对象的引用。</p>
<h3 id="private"><a href="#private" class="headerlink" title="private"></a>private</h3><p>私有，权限修饰符，用于修饰类中的成员（成员变量，成员函数）私有只在本类中有效。<br>私有仅仅是封装的一种表现形式。</p>
<h3 id="static"><a href="#static" class="headerlink" title="static"></a>static</h3><p>是一个修饰符，用于修饰成员（成员变量，成员函数）<br>被静态修饰的变量不在堆内存中。<br>当成员被静态修饰后，就多了一个调用方式，除了可以被对象调用外，还可以直接被类名调用，类名.静态成员</p>
<p>static的特点</p>
<ol>
<li>随着类的加载而加载<br>当类加载到内存时，static修饰的东西就在内存中开辟好了空间。<br>随着类的消失而消失。</li>
<li>优先于对象的存在</li>
<li>被所有对象所共享</li>
<li>可以直接被类名调用    </li>
</ol>
<p>为啥不把所有都定义成static?<br>对内存的消耗比较大。<br>静态是先存在的，类是后存在的。</p>
<h4 id="示例变量和类变量的区别。"><a href="#示例变量和类变量的区别。" class="headerlink" title="示例变量和类变量的区别。"></a>示例变量和类变量的区别。</h4><p>主函数是静态的<br>主函数的定义:</p>
<ul>
<li>public：代表着该函数访问权限是最大的了。</li>
<li>static：代表主函数随着类的加载就已经存在了。</li>
<li>void：主函数没有具体的返回值。</li>
<li>main：不是关键字，但是是特殊的单词，可以被JVM识别。<br>函数的参数：（String[] arr）:函数的参数，该数组中的元素是字符串，字符串类型的数组。<br>主函数是固定格式的：JVM识别<br>JVM在调用主函数时，传入的是new String[0]<h5 id="存放位置："><a href="#存放位置：" class="headerlink" title="存放位置："></a>存放位置：</h5></li>
<li>类变量随着类的加载存在于方法区中。</li>
<li>实例变量随着对象的建立而存在于对堆内存中。<h5 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h5></li>
<li>类变量生命周期最长，随着类的消失而消失</li>
<li>实例变量生命周期随着对象的消失而消失<h5 id="静态使用注意事项"><a href="#静态使用注意事项" class="headerlink" title="静态使用注意事项"></a>静态使用注意事项</h5></li>
<li> 静态方法只能访问静态成员</li>
<li>非静态方法可以访问静态成员</li>
</ul>
<h4 id="什么时候定义静态变量"><a href="#什么时候定义静态变量" class="headerlink" title="什么时候定义静态变量"></a>什么时候定义静态变量</h4><ul>
<li>当对象中出现共享数据时，该数据被静态所修饰、</li>
<li>对象中的特有数据要定义成非静态存在于堆内存中。</li>
</ul>
<h4 id="什么时候定义静态函数"><a href="#什么时候定义静态函数" class="headerlink" title="什么时候定义静态函数"></a>什么时候定义静态函数</h4><ul>
<li>当功能内部没有访问到非静态数据（对象特有数据）</li>
</ul>
<p>当一个java文件中调用了另一个java文件，虚拟机会在当前目录或指定目录下寻找类名.java 文件，并先把他编译。<br>不用暴露的类方法全部私有化。</p>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>提高代码复用性<br>让类与类之间产生关系<br>关键字 ：extends</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">class X extends Y&#123;<br>	int a &#x3D; 10;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>X 是 Y 的子类，Y也叫超类<br>不要为了获取其他类的功能瞎鸡巴继承，必须是类与类之间有所属关系才行。</p>
<p>子类调用方式：<br>访问本类中的变量 ：this.a<br>访问父类中的变量：super.a</p>
<p>子类一定要访问父类中的构造函数，因为子类要看父类是如何对数据初始化的。</p>
<h3 id="final"><a href="#final" class="headerlink" title="final"></a>final</h3><ol>
<li>最终，作为一个修饰符</li>
<li>可以修饰类，变量，函数</li>
<li>被final修饰的不能被继承，被覆写</li>
<li>被final修饰的只能赋值一次 </li>
</ol>
<h3 id="abstract"><a href="#abstract" class="headerlink" title="abstract"></a>abstract</h3><ol>
<li>抽象：看不懂</li>
<li>抽象方法一定定义在抽类中</li>
<li>抽象类不可以用new创建对象，因为调用抽象方法没意义 </li>
</ol>
<h3 id="接口：interface"><a href="#接口：interface" class="headerlink" title="接口：interface"></a>接口：interface</h3><ol>
<li>接口不能创建对象，子类将接口方法全部覆盖后，才可以被实例化</li>
<li>一个类可以同时实现多个接口<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs plain">interface X&#123;<br>	<br>&#125;<br><br>interface Z extends X&#123;<br><br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="实现：implements"><a href="#实现：implements" class="headerlink" title="实现：implements"></a>实现：implements</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">class Y implements X&#123;<br><br>&#125;<br></code></pre></td></tr></table></figure></li>
</ol>
<h3 id="内部类"><a href="#内部类" class="headerlink" title="内部类"></a>内部类</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs plain">class a&#123;<br>	class b&#123; &#x2F;&#x2F;这是内部类<br>		public void c()&#123;<br>			<br>		&#125;<br>	&#125;<br>&#125;<br>class a&#123;<br>	static class b&#123; &#x2F;&#x2F;这是静态内部类<br>		public void c()&#123;<br>			<br>		&#125;<br>	&#125;<br>&#125;<br>a.b obj &#x3D; new a().new b(); &#x2F;&#x2F;调用内部类<br>obj.c();<br>a.b obj &#x3D; new a.b(); &#x2F;&#x2F;调用静态内部列<br>obj.c();<br></code></pre></td></tr></table></figure>
<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><ul>
<li>事物存在的多种体现形态</li>
<li>代码体现：父类的引用指向了自己的子类对象<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs plain">public class Test &#123;<br><br>	public static void main(String[] args) &#123;<br>		go_eat(new dog());<br>	&#125;<br>	<br>	public static void go_eat(Annimo a) &#123;<br>		a.eat();<br>	&#125;<br>	<br>&#125;<br><br>abstract class Annimo&#123;<br>	abstract void eat();<br>&#125;<br><br>class dog extends Annimo&#123;<br>	public void eat() &#123;<br>		System.out.print(&quot;666&quot;);<br>	&#125;<br>	<br>&#125;<br><br></code></pre></td></tr></table></figure></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">Annimo c &#x3D; new dog();&#x2F;&#x2F; 向上转型<br>先把对象向上转形，在向下转型，不能直接向下转型<br></code></pre></td></tr></table></figure>

<p>在多态中成员函数的特点：<br>在编译时期：参阅引用形变量所属的类中是否有调用的方法。如果有，编译通过，如没有，编译失败。<br>在运行时期：参阅对象所属的类中是否有调用的方法。 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs plain">public class Test &#123;<br>	public static void main(String[] args) &#123;<br>		Fu t &#x3D; new Zi();<br>		t.m1();<br>		t.m2();<br>		t.m3();<br>	&#125;<br>&#125;<br><br>class Fu&#123;<br>	void m1()&#123;<br>		System.out.println(&quot;fu 1&quot;);<br>	&#125;<br>	void m2() &#123;<br>		System.out.println(&quot;fu 2&quot;);<br>	&#125;<br>	void m3() &#123;<br>		System.out.println(&quot;fu 3&quot;);<br>	&#125;<br>&#125;<br><br>class Zi extends Fu&#123;<br>	void m1()&#123;<br>		System.out.println(&quot;zi 1&quot;);<br>	&#125;<br>	void m2() &#123;<br>		System.out.println(&quot;zi 2&quot;);<br>	&#125;<br>&#125;<br><br>&#x2F;&#x2F;输出结果：<br>zi 1<br>zi 2<br>fu 3<br></code></pre></td></tr></table></figure>

<h3 id="接口的好处"><a href="#接口的好处" class="headerlink" title="接口的好处"></a>接口的好处</h3><p>降低程序的耦合性</p>
<h3 id="Object-是所有对象的直接或者间接-的父类"><a href="#Object-是所有对象的直接或者间接-的父类" class="headerlink" title="Object 是所有对象的直接或者间接 的父类"></a>Object 是所有对象的直接或者间接 的父类</h3><h3 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h3><p>异常是程序中出现不正常的情况。<br>异常的由来：问题也是现实生活中的一个具体的事物，也可以通过java类的形式进行描述，其实就是java对不正常描述后的体现</p>
<p>对于问题的划分，分为两种，一种是严重的问题，一种是非严重的问题，对于严重的，java通过Error类进行描述，对于非严重的，java通过Exception 类进行描述。</p>
<p>对于Error 一般不编写代码进行处理</p>
<p>异常的处理：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs plain">try&#123;<br>	需要被检测的代码<br>&#125;catch(异常类，变量)&#123;<br>	异常处理代码：(处理方式)<br>&#125;finally&#123;<br>	 <br>&#125;<br>&#x2F;&#x2F;或者使用throws直接抛出异常<br>class Demo&#123;<br>	int div(int a,int b) throws Exception&#123;<br>		return a&#x2F;b;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="throw-和-throws的区别"><a href="#throw-和-throws的区别" class="headerlink" title="throw 和 throws的区别"></a>throw 和 throws的区别</h4><pre><code>throws用在函数外，thtow用在函数内
</code></pre>
<p>自定义错误</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;定义错误类<br>class Error1Exception extends Exception&#123;<br>	Error1Exception(String msg)&#123;<br>		super(msg);<br>	&#125;<br>&#125;<br><br><br>class T1<br>&#123;<br>	&#x2F;&#x2F;抛出异常<br>	static public void run(int a) throws Error1Exception&#123;<br>		if(a&#x3D;&#x3D;1) &#123;<br>			throw new Error1Exception(&quot;错误一&quot;);<br>		&#125;<br>	&#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="字符串对象String"><a href="#字符串对象String" class="headerlink" title="字符串对象String"></a>字符串对象String</h3><p>定义任何一个字符串都是new了一个对象<br>以下两者相同：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">String a &#x3D; new String();<br>String a &#x3D; &quot;&quot;;<br></code></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">String a &#x3D; &quot;aaa&quot;; &#x2F;&#x2F;定义a为aaa<br>a &#x3D; &quot;bbb&quot;; &#x2F;&#x2F;aaa的值没有改变，而是a指向了bbb<br></code></pre></td></tr></table></figure>
<p>equals方法用于判断两者的内存地址值是否相等，但String覆写了该方法，用于判断字符串是否相同</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs plain">str.split(&quot;,&quot;); &#x2F;&#x2F;用逗号分割字符串，返回数组<br>str.substring(start,stop); &#x2F;&#x2F;截取从statr 到stop处所有字符。含头不含尾<br>str.toUpperCase(); &#x2F;&#x2F;将字符串转大写<br>str.toLowerCase(); &#x2F;&#x2F;将字符串转换小写<br>str.trim(); &#x2F;&#x2F;去除字符串两端的多个空格<br>str.compareTo(&quot;String&quot;) &#x2F;&#x2F;对两个字符串进行自然顺序的比较<br></code></pre></td></tr></table></figure>


<h3 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">a instanceof b;<br>判断a类是不是b类的实例对象<br></code></pre></td></tr></table></figure>

<p>问题：S1和S2有什么区别？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">String s1 &#x3D; &quot;abc&quot;;<br>String s2 &#x3D; new String(&quot;abc&quot;);<br></code></pre></td></tr></table></figure>
<p>答：s1中有一个对象”abc”，s2中有两个对象new String() 和 “abc”。</p>
<h3 id="package"><a href="#package" class="headerlink" title="package"></a>package</h3><p>对类文件进行分类管理<br>给类提供多层命名空间<br>写在程序文件的第一行<br>类名的全称是 包名.类名<br>包也是一种封装形式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plain">package pack;<br></code></pre></td></tr></table></figure>
<h4 id="包与包之间的访问"><a href="#包与包之间的访问" class="headerlink" title="包与包之间的访问"></a>包与包之间的访问</h4><h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><ul>
<li>为什么出现集合类？<ul>
<li>对象多了，就用集合来存，集合是存储对象的一种最常用的形式。</li>
</ul>
</li>
<li>数组和集合类都是容器，有何不同？<ul>
<li>数组虽然可以存储对象，但是长度是固定的；集合长度是可变的，数组中可以存储基本数据类型，集合只能存储对象。</li>
</ul>
</li>
<li>集合的特点<ul>
<li>集合之用于存储对象，集合长度是可变的，集合可以存储不同类型的变量。</li>
</ul>
</li>
</ul>
<h4 id="单列集合-Collection"><a href="#单列集合-Collection" class="headerlink" title="单列集合 Collection"></a>单列集合 Collection</h4><p>目录：</p>
<ul>
<li><p>Collection</p>
<ul>
<li>List<ul>
<li>ArrayList</li>
<li>LinkedList</li>
<li>Vector</li>
</ul>
</li>
<li>Set<ul>
<li>HashSet</li>
<li>TreeSet</li>
</ul>
</li>
</ul>
</li>
<li><p>为什么会出现这么多容易呢？</p>
<ul>
<li>因为每一个容器对数据的存储方式不同。这个存储方式称之为：数据结构。     </li>
</ul>
</li>
<li><h5 id="List-有索引，可以存储重复元素、可以保证文件存取顺序"><a href="#List-有索引，可以存储重复元素、可以保证文件存取顺序" class="headerlink" title="List:有索引，可以存储重复元素、可以保证文件存取顺序"></a>List:有索引，可以存储重复元素、可以保证文件存取顺序</h5><ul>
<li>ArrayList : 底层是数组实现的，查询快，增删慢。</li>
<li>LinkedList : 底层是链表实现的，查询慢，增删快。</li>
</ul>
</li>
<li><h5 id="Set-无索引，不可以存取重复元素，存取无序。"><a href="#Set-无索引，不可以存取重复元素，存取无序。" class="headerlink" title="Set : 无索引，不可以存取重复元素，存取无序。"></a>Set : 无索引，不可以存取重复元素，存取无序。</h5><ul>
<li>HashSet : 底层是哈希表 + （红黑树）实现的，无索引，不可以存储重复元素，  存取无序。</li>
<li>LinkedHashSet : 底层是哈希表 + 链表实现的，无索引，不可以存储重复元素、可以保证存取顺序。</li>
<li>TreeSet : 底层是二叉树实现，一般用于排序。<br>方法：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs plain">创建集合：<br>Collection&lt;String&gt; list &#x3D; new ArrayList&lt;String&gt;();<br>共性方法：<br>list.add(Objeci o); &#x2F;&#x2F;向集合中添加一个元素<br>list1.addAll(list2);&#x2F;&#x2F;把list2的所有元素添加至list1<br>list2.clear();&#x2F;&#x2F;清空集合的所有元素<br>list.remove(&quot;值&quot;); &#x2F;&#x2F;删除list中的一个值，返回值为布尔类型，其中&quot;值&quot;也可以是一个集合，会删除集合中的所有元素<br>list.isEmpty(); &#x2F;&#x2F;判断集合是不是空<br>lsit.contains(); &#x2F;&#x2F;判断集合中是否包含某个元素，返回布尔类型。<br>list.size(); &#x2F;&#x2F;输出集合数量<br><br>list集合方法：<br>特点：存取有序，有索引，可存取重复元素。<br>list.add(index,obj); &#x2F;&#x2F;把obj添加至index缩影位置<br>list.addAll(index,Collection); 把集合Collection所有元素添加至index的位置<br>list.get(index); &#x2F;&#x2F;	获取index索引处的内容	<br>list.remove(index); &#x2F;&#x2F;返回index索引处的位置，并删除<br>list.set(index,obj); &#x2F;&#x2F;将集合index处的内容替换成obj,返回被替换的元素<br>list.indexOf(obj);&#x2F;&#x2F;查找obj在集合中出现的位置<br>list.lastIndexOf(obj); &#x2F;&#x2F;查找obj在集合中最后一次出现的位置<br>list.subList(fromIndex,toIndex); &#x2F;&#x2F; 返回集合开始（包括）到结束（不包括）的所有元素的子集合<br>list.toArray(); &#x2F;&#x2F;将集合转换为数组<br>&#x2F;&#x2F;创建list集合对象<br>List&lt;String&gt; list &#x3D; new ArrayList&lt;String&gt;();<br></code></pre></td></tr></table></figure>
<h4 id="HashSet"><a href="#HashSet" class="headerlink" title="HashSet"></a>HashSet</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;使用HashSet<br>		HashSet list &#x3D; new HashSet();<br>		list.add(&quot;AAA&quot;);<br>		list.add(&quot;BBB&quot;);<br>		list.add(&quot;CCC&quot;);<br>		Iterator it &#x3D; list.iterator();<br>		while (it.hasNext()) &#123;<br>			System.out.println(it.next());<br>		&#125;<br>	&#x2F;&#x2F;输出为无序。<br>HashSet是如何保证元素唯一性呢？<br>是通过两个方法hashCode和equals来完成。<br>如果元素的HashCode值相同，才会判断equals是否为true。<br>如果元素的hashCode值不同，不会调用equals.<br></code></pre></td></tr></table></figure>
<h4 id="TreeSet"><a href="#TreeSet" class="headerlink" title="TreeSet"></a>TreeSet</h4>TreeSet：可以对Set集合中的元素进行排序。<br>在使用TreeSet时，往里面存的对象必须具备比较性<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;直接存元素<br>TreeSet tree &#x3D; new TreeSet();<br>tree.add(&quot;333&quot;);<br>tree.add(&quot;222&quot;);<br>tree.add(&quot;111&quot;);<br>Iterator it &#x3D; tree.iterator();<br>while(it.hasNext()) &#123;<br>	System.out.println(it.next());<br>&#125;<br></code></pre></td></tr></table></figure>
如果元素不可比较<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs plain">		TreeSet tree &#x3D; new TreeSet();<br>		tree.add(new Stu(&quot;张三&quot;,20));<br>		tree.add(new Stu(&quot;李四&quot;,31));<br>&#x2F;&#x2F;报错<br>Exception in thread &quot;main&quot; java.lang.ClassCastException: class zhf.demo.Stu cannot be cast to class java.lang.Comparable (zhf.demo.Stu is in unnamed module of loader &#39;app&#39;; java.lang.Comparable is in module java.base of loader &#39;bootstrap&#39;)<br>	at java.base&#x2F;java.util.TreeMap.compare(TreeMap.java:1291)<br>	at java.base&#x2F;java.util.TreeMap.put(TreeMap.java:536)<br>	at java.base&#x2F;java.util.TreeSet.add(TreeSet.java:255)<br>	at zhf.demo.T11.main(T11.java:11)<br></code></pre></td></tr></table></figure>
如果要结局，上文中Stu对象要实现Comparable接口，覆写其中的compareTo()方法，让Stu类强制具备可比较性<br>compareTo方法：</li>
</ul>
</li>
<li><p>如果指定的数与参数相等返回0。</p>
</li>
<li><p>如果指定的数小于参数返回 -1。</p>
</li>
<li><p>  如果指定的数大于参数返回 1。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs plain">class Stu implements Comparable&#123;<br>	String name;<br>	int age;<br>	Stu(String name,int age)&#123;<br>		this.name &#x3D; name;<br>		this.age &#x3D; age;<br>	&#125;<br>	public int compareTo(Object obj)&#123;<br>		return 0; <br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>当主要条件相同时，一定要判断次要条件。<br>当元素不具备比较性，这时需要让元素自身具备比较性 ，将比较器作为参数传递给TreeSet对象。<br>当两种排序都存在时，以比较器为主</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">TreeSet(Comparator&lt;? super E&gt; comparator)<br>构造一个新的，空的树集，根据指定的比较器进行排序。<br></code></pre></td></tr></table></figure>
<p>自定义比较器是Comparable中的compare方法，覆写的方法是compareTo方法</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs plain"><br>	public static void main(String[] args) &#123;<br>		&#x2F;&#x2F; TODO Auto-generated method stub<br>		TreeSet tree &#x3D; new TreeSet(new MyComparable()); &#x2F;&#x2F;传入比较器<br>		tree.add(new Stu(&quot;李四&quot;,20));<br>		tree.add(new Stu(&quot;张三&quot;,31));<br>		Iterator it &#x3D; tree.iterator();<br>		while(it.hasNext()) &#123;<br>			Stu s &#x3D; (Stu)it.next();<br>			System.out.println(s.name);<br>		&#125;<br>	&#125;<br><br>&#125;<br><br>class Stu&#123;<br>	String name;<br>	int age;<br>	Stu(String name,int age)&#123;<br>		this.name &#x3D; name;<br>		this.age &#x3D; age;<br>	&#125;<br>&#125;<br><br>class MyComparable implements Comparator&#123;  &#x2F;&#x2F;定义一个类实现Comparator接口，覆盖compar方法<br>	public int compare(Object o1,Object o2) &#123;<br>		Stu s1 &#x3D; (Stu)o1;<br>		Stu s2 &#x3D; (Stu)o2;<br>		int num &#x3D; s1.name.compareTo(s2.name); &#x2F;&#x2F;比较姓名<br>		return num;<br>	&#125;<br></code></pre></td></tr></table></figure>
<p>自定义比较器例子：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs plain">public static void main(String[] args) &#123;<br>		&#x2F;&#x2F; TODO Auto-generated method stub<br>		TreeSet tree &#x3D; new TreeSet(new Mybj());<br>		tree.add(new Stu1(&quot;张三&quot;,20));<br>		tree.add(new Stu1(&quot;李四&quot;,10));<br>		Iterator it &#x3D; tree.iterator();<br>		while(it.hasNext()) &#123;<br>			Stu1 a &#x3D; (Stu1)it.next();<br>			System.out.println(a.age);<br>		&#125;<br>	&#125;<br><br>&#125;<br>class Stu1&#123;<br>	int age;<br>	String name;<br>	Stu1(String name,int age)&#123;<br>		this.name &#x3D; name;<br>		this.age &#x3D; age;<br>	&#125;<br>&#125;<br>class Mybj implements Comparator &#123;<br>	public int compare(Object o1,Object o2) &#123;<br>		Stu1 a &#x3D; (Stu1)o1;<br>		Stu1 b &#x3D; (Stu1)o2;<br>		if(a.age - b.age&lt;0) &#123;<br>			return -1;<br>		&#125;<br>		return 1;<br>	&#125;<br></code></pre></td></tr></table></figure>
<h4 id="双列集合"><a href="#双列集合" class="headerlink" title="双列集合"></a>双列集合</h4><ul>
<li><h5 id="Map"><a href="#Map" class="headerlink" title="Map"></a>Map</h5><ul>
<li>Hashtable<ul>
<li>Properties</li>
</ul>
</li>
<li>HashMap<ul>
<li>LinkHashMap</li>
</ul>
</li>
<li>TreeMap</li>
</ul>
</li>
</ul>
<h4 id="遍历集合-Iterator"><a href="#遍历集合-Iterator" class="headerlink" title="遍历集合 Iterator"></a>遍历集合 Iterator<E></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">hasNext(); &#x2F;&#x2F;如果仍有元素可以迭代，则返回true<br>next(); &#x2F;&#x2F;返回下一个迭代元素<br>remove(); &#x2F;&#x2F;把元素从迭代器中删除<br></code></pre></td></tr></table></figure>
<p>代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;迭代器遍历集合<br>Collection&lt;String&gt; a &#x3D; new ArrayList&lt;&gt;();<br>a.add(&quot;aa&quot;);<br>a.add(&quot;bb&quot;);<br>Iterator&lt;String&gt; it &#x3D; a.iterator(); &#x2F;&#x2F; 获取迭代器对象<br>boolean b &#x3D; it.hasNext(); &#x2F;&#x2F;判断是否还有下一个元素<br>System.out.println(b); <br>String e &#x3D; it.next(); &#x2F;&#x2F;获取下一个元素<br>System.out.println(e); &#x2F;&#x2F;aa<br>e &#x3D; it.next();  &#x2F;&#x2F;获取下一个元素<br>System.out.println(e); &#x2F;&#x2F;bb<br></code></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;迭代器删除集合<br>		while (it.hasNext()) &#123;<br>		String e &#x3D; it.next();<br>		if(e.equals(&quot;bb&quot;)) &#123;<br>			it.remove();<br>		&#125;<br>	&#125; <br>	<br></code></pre></td></tr></table></figure>
<h4 id="反向迭代"><a href="#反向迭代" class="headerlink" title="反向迭代"></a>反向迭代</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs plain">ListIterator lit &#x3D;  list.listIterator(list.size()); &#x2F;&#x2F;反向迭代的迭代器<br>		while(lit.hasPrevious())&#123; &#x2F;&#x2F;判断前一个是否还有元素<br>			Object obj &#x3D; lit.previous(); &#x2F;&#x2F;返回元素<br>			System.out.println(obj);<br>		&#125;<br></code></pre></td></tr></table></figure>
<h4 id="List特有的迭代器ListIterator"><a href="#List特有的迭代器ListIterator" class="headerlink" title="List特有的迭代器ListIterator"></a>List特有的迭代器ListIterator</h4><p>List集合特有的迭代器，ListIterator 是 Iterator  的子接口。<br>在迭代时，不可以通过集合对象的方法操作集合中的元素。<br>因为会发生ConcurrentModificationException异常。</p>
<p>可是Iterator中的方法是有限的，如果想要添加等操作，就要用到ListIterator迭代器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">list.set();&#x2F;&#x2F;修改<br>list.add();&#x2F;&#x2F;添加<br>list.next();&#x2F;&#x2F;返回列表中的下一个元素，并且前进光标位置。<br></code></pre></td></tr></table></figure>
<h4 id="foreach遍历集合"><a href="#foreach遍历集合" class="headerlink" title="foreach遍历集合"></a>foreach遍历集合</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F; 使用增强for遍历数组<br>String[] arr &#x3D; &#123;&quot;aa&quot;,&quot;bb&quot;,&quot;cc&quot;,&quot;dd&quot;&#125;; <br>for(String s : arr)&#123;<br>System.out.println(s);<br>&#125;<br><br>JDK8以后：<br>list.forEach((String e)-&gt;P&#123;System.out.println(e)&#125;);<br></code></pre></td></tr></table></figure>

<h4 id="Map-1"><a href="#Map-1" class="headerlink" title="Map"></a>Map</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs plain">map.keySet();&#x2F;&#x2F;返回键的集合<br>map.value();&#x2F;&#x2F;返回值的集合<br>map.put(键,值);&#x2F;&#x2F;添加put(键,值)<br>map.entrySet(); &#x2F;&#x2F;<br>&#x2F;&#x2F; 实例<br>		Map map &#x3D; new HashMap(); &#x2F;&#x2F;创建HashMap对象<br>		map.put(&quot;1&quot;, &quot;Jack&quot;); &#x2F;&#x2F;添加put(键,值)<br>		map.put(&quot;2&quot;, &quot;Rose&quot;);<br>		map.put(&quot;1&quot;, &quot;Lucy&quot;);<br>		<br>		&#x2F;&#x2F;第一种方法，获取值和键的集合<br>		Set keySets &#x3D; map.keySet();  <br>		Iterator it &#x3D; keySets.iterator();<br>		while(it.hasNext())&#123;<br>			Object k &#x3D; it.next();<br>			Object value &#x3D; map.get(k);<br>			System.out.println(k + &quot;:&quot; + value);<br>		&#125;<br>		&#x2F;&#x2F;第二种方法，获取值的集合<br>		Collection vals &#x3D; map.values();<br>		Iterator it1 &#x3D; vals.iterator();<br>		while(it1.hasNext())&#123;<br>			Object value &#x3D; it1.next();<br>			System.out.println(value);<br>		&#125;<br>		<br>		&#x2F;&#x2F;第三种方式<br>		Set enSet &#x3D; map.entrySet();<br>		Iterator it2  &#x3D; enSet.iterator();<br>		while(it2.hasNext())&#123;<br>			Map.Entry entry &#x3D; (Map.Entry)(it2.next());<br>			Object key &#x3D; entry.getKey();<br>			Object value &#x3D; entry.getValue();<br>			System.out.println(key + &quot;:&quot; + value);<br>		&#125;<br></code></pre></td></tr></table></figure>
<h4 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h4><p>JDK1.5版本后推出的新特性，用于解决安全问题，是一个安全机制。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs plain">ArrayList&lt;String&gt; arr &#x3D; new ArrayList&lt;String&gt;();<br>arr.add(&quot;5&quot;);<br>arr.add(5); &#x2F;&#x2F;报错<br></code></pre></td></tr></table></figure>
<p>泛型类<br>泛型方法<br>泛型静态方法<br>泛型接口</p>
<p>在不确定集合中存储元素的类型时，可以用？来代替</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plain">ArrayList&lt;?&gt; arr &#x3D; new ArrayList&lt;?&gt;();<br></code></pre></td></tr></table></figure>
<p>? : 通配符，也可以理解为占位符<br>？extends E : 可以接收E类型或E的子类型</p>
<h4 id="基本数据类型的包装类"><a href="#基本数据类型的包装类" class="headerlink" title="基本数据类型的包装类"></a>基本数据类型的包装类</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs plain">byte Byte<br>short short<br>int Integer<br>long Longer<br>boolean Boolean<br></code></pre></td></tr></table></figure>

<h3 id="JAVA-IO"><a href="#JAVA-IO" class="headerlink" title="JAVA IO"></a>JAVA IO</h3><p>IO流用来处理设备之间的数据传输<br>Java对数据的操作是通过流的方式<br>Java用于操作流的对象都子IO包中<br>流按操作数据分为两种 -  字节流与字符流<br>流按流向分为 - 输入流，输出流</p>
<h4 id="流"><a href="#流" class="headerlink" title="流"></a>流</h4><ul>
<li>字符流<ul>
<li>Reader</li>
<li>Writer</li>
</ul>
</li>
<li>字节流<ul>
<li>InputSteam</li>
<li>OutputStream</li>
</ul>
</li>
<li>找到一个专门用于操作文件的Writer子类对象 。FileWriter。后缀名是父类名，前缀名是该流对象的功能。<br>由这四个类派生粗来的子类名称都是以其付类名作为子类名的后缀<br>在硬盘上创建一个文件，并写入一些内容：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs plain">&#x2F;&#x2F;创建一个FileWriter对象，该对象一被初始化就必须要明确被操作的文件。<br>FileWriter fw &#x3D; new FileWriter(&quot;D:&#x2F;1.txt&quot;);<br>&#x2F;&#x2F;如果该目录下有同名文件，将被覆盖。<br>&#x2F;&#x2F;该步就是在明确数据要存放的目的地。<br>&#x2F;&#x2F;第二步，使用write方法将字符串写入到流中。<br>fw.write(&quot;abcde&quot;);<br>fw.flush();&#x2F;&#x2F;刷新流的缓冲<br>fw.write(&quot;bcdef&quot;);<br>fw.flush();<br>fw.close(); &#x2F;&#x2F;关闭流资源，但是关闭之前会刷新一次内部缓冲中的数据，flush刷新后可以继续写入，close刷新后将会流关闭。<br> <br></code></pre></td></tr></table></figure>
IO异常的处理方式<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs plain">FileWriter fw &#x3D; null; &#x2F;&#x2F;如果在try中定义变量，那么该变量为局部变量，在finally中不能调用，所以在外部创建全局变量 <br>try &#123;<br>fw &#x3D; new FileWriter(&quot;D:&#x2F;1.txt&quot;);<br>&#125;catch(IOException e)&#123;<br>	System.out.println(e.toString());<br>&#125;finally &#123;<br>	try &#123;<br>		if(fw!&#x3D;null) &#123;<br>			fw.close();<br>		&#125;<br>	&#125;catch(IOException e) &#123;<br>		System.out.println(e.toString());<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
文件的连续写入：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs plain">FileWriter fw &#x3D; new FileWriter(&quot;D:&#x2F;1.txt&quot;,true);<br>fw.write(&quot;aaa&quot;);<br>fw.write(&quot;bbb&quot;);<br>fw.write(&quot;\r\n&quot;);&#x2F;&#x2F;换行<br>fw.write(&quot;bbb&quot;);<br>fw.close(); <br></code></pre></td></tr></table></figure>
文件的读取<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><code class="hljs plain">	public static void main(String arg[]) throws Exception&#123;<br>		FileReader reader &#x3D; new FileReader(&quot;1.txt&quot;);<br>		FileWriter write &#x3D; new FileWriter(&quot;2.txt&quot;,true);<br>		int a;<br>		while((a&#x3D;reader.read())!&#x3D;-1)&#123;<br>			write.write(a);<br>		&#125;<br>		reader.close(); &#x2F;&#x2F;关闭读取<br>		write.flush(); &#x2F;&#x2F;刷新内存<br>		write.close(); &#x2F;&#x2F;写入关闭<br>	&#125;<br>&#125;<br><br>&#x2F;&#x2F;按行读取<br>	public static void main(String arg[]) throws Exception&#123;<br>		FileReader reader &#x3D; new FileReader(&quot;1.txt&quot;);  &#x2F;&#x2F;读取文件流<br>		BufferedReader br &#x3D; new BufferedReader(reader); &#x2F;&#x2F;BufferedReader<br>		<br>		FileWriter write &#x3D; new FileWriter(&quot;2.txt&quot;); &#x2F;&#x2F;写入文件流<br>		BufferedWriter bw &#x3D; new BufferedWriter(write);&#x2F;&#x2F;BufferedWriter<br>		<br>		String str;<br>		while((str &#x3D;br.readLine())!&#x3D;null)&#123;<br>			bw.write(str);<br>			bw.newLine(); &#x2F;&#x2F;新的一行<br>		&#125;<br>		br.close();<br>		bw.flush();<br>		bw.close();<br>	&#125;<br>&#x2F;&#x2F;多个字符读取<br>		FileReader fr &#x3D; new FileReader(&quot;D:&#x2F;1.txt&quot;);<br>		&#x2F;&#x2F;定义一个字符数组用于存储读到的字符。<br>		char[] buf &#x3D; new char[20];<br>		int num &#x3D; 0;<br>		&#x2F;&#x2F;该read(char[])返回的是读到的字符个数。<br>		while((num&#x3D;fr.read(buf))!&#x3D;-1) &#123;<br>			&#x2F;&#x2F;从0号角标开始取，取字符个数个字符。<br>			System.out.print(new String(buf,0,num)); <br>		&#125;<br>&#x2F;&#x2F;文本文件拷贝<br><br>		&#x2F;&#x2F;创建目的地。<br>		FileWriter fw &#x3D; new FileWriter(&quot;D:&#x2F;2.txt&quot;);<br>		&#x2F;&#x2F;与已有文件关联<br>		FileReader fr &#x3D; new FileReader(&quot;D:&#x2F;1.txt&quot;);<br>		int ch &#x3D; 0;<br>		while((ch&#x3D;fr.read())!&#x3D;-1) &#123;<br>			fw.write(ch);<br>		&#125;<br>		fw.close();<br>		fr.close();<br>&#x2F;&#x2F;文本文件拷贝方法2<br>		FileWriter fw &#x3D; null;<br>		FileReader fr &#x3D; null;<br>		try &#123;<br>			fw &#x3D; new FileWriter(&quot;D:&#x2F;1.txt&quot;);<br>			fr &#x3D; new FileReader(&quot;D:&#x2F;2.txt&quot;);<br>			char[] buf &#x3D; new char[1024];<br>			int len &#x3D; 0;<br>			while((len&#x3D;fr.read(buf))!&#x3D;-1) &#123;<br>				fw.write(buf,0,len);<br>			&#125;<br>		&#125;catch (IOException e) &#123;<br>			&#x2F;&#x2F; TODO: handle exception<br>			System.out.println(&quot;文件读写异常&quot;);<br>		&#125;finally &#123;<br>			if(fr!&#x3D;null) &#123;<br>				try &#123;<br>					fr.close();<br>				&#125; catch (Exception e2) &#123;<br>					System.out.println(e2.toString());<br>				&#125;<br>				try &#123;<br>					fw.close();<br>				&#125; catch (Exception e2) &#123;<br>					System.out.println(e2.toString());<br>				&#125;<br>				<br>			&#125;<br>		&#125;<br>&#x2F;&#x2F;创建文件夹<br>		File file &#x3D; new File(&quot;FFF&quot;);<br>		file.mkdir();<br>&#x2F;&#x2F;创建文件<br>		file &#x3D; new File(&quot;FFF&#x2F;9.txt&quot;);<br>		file.createNewFile();<br></code></pre></td></tr></table></figure>
知识点：<br>File 类不能读写文件，只能创建文件和文件夹<br>Reader是读取数据文件的抽象类<br>字符输出流类都是Writer抽象类的子类<br>字符流中的数据以16位字符为单位进行读写<br>当输入一个字节流时，需要实现DataInput接口<br>使用File类不能改变当前目录<br>可以用来描述文件或者文件夹的类是File<br>File类中，创建多级目录，可以通过mkdirs()方法<br>InputStream等等是抽象类<br>read()方法的返回值如果为-1,表示到流的末尾<br>ead(char[] cbuf)方法表示将读到的多个字符存入字符数组cbuf中<br>FileWriter类直接继承InputStreamReader类<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs plain">Java线程<br></code></pre></td></tr></table></figure>
进程：是一个正在执行中的程序<br>每一个进程都有一个执行顺序，该顺序是一个执行路径，或者叫一个控制单元<br>线程：就是进程中的一个独立控制单元，线程在控制着进程的执行。</li>
</ul>
<p>JVM     启动的时候会有一个java.exe的进程<br>该进程中至少有一个线程负责java程序的执行。<br>这个线程的代码存放在main中，该线程也称为主线程<br>JVM启动不只一个线程，还有负责垃圾回收机制的线程</p>
<p>java已经提供了对线程这类事物的描述，就是Thread类。<br>创建线程的第一种方式：</p>
<ol>
<li>定义继承Thread类</li>
<li>覆写Thread类中的run方法。<br> 目的：将自定义代码存放在run方法中</li>
<li>调用线程的start() 方法。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs plain"><br>public class T21 &#123;<br><br>	public static void main(String[] args) &#123;<br>		&#x2F;&#x2F; TODO Auto-generated method stub<br>		ThreadDemo01 d1 &#x3D; new ThreadDemo01(); &#x2F;&#x2F;创建线程<br>		d1.start(); &#x2F;&#x2F;启动线程<br>		for(int i&#x3D;0;i&lt;&#x3D;60;i++) &#123;  &#x2F;&#x2F;主线程<br>			System.out.println(&quot;M----&quot; + i );<br>		&#125;<br>	&#125;<br><br>&#125;<br><br>class ThreadDemo01 extends Thread&#123;<br>	public void run()&#123; &#x2F;&#x2F;覆写run方法<br>		for(int i &#x3D;0;i&lt;&#x3D;60;i++) &#123;<br>			System.out.println(&quot;T----&quot; + i);<br>		&#125;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>方法二：</p>
<ol>
<li>定义类实现Runnable接口</li>
<li>覆盖Runnable接口中的run方法</li>
<li>将Runnable接口的子对象作为实际参数传递给Thread的构造函数</li>
<li>调用Thread类的start方法开启线程并调用Runnable接口子类的run方法</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs plain">public class T23 &#123;<br><br>	public static void main(String[] args) &#123;<br>		&#x2F;&#x2F; TODO Auto-generated method stub<br>		Tdemo03 a1 &#x3D; new Tdemo03();  &#x2F;&#x2F;创建对象<br>		Thread t1 &#x3D; new Thread(a1); &#x2F;&#x2F;创建线程，传入对象	<br>		Thread t2 &#x3D; new Thread(a1);<br>		t1.start();<br>		t2.start();<br>	&#125;<br>&#125;<br><br>class Tdemo03 implements Runnable&#123;<br>	public void run() &#123;<br>		for(int i&#x3D;0;i&lt;&#x3D;60;i++) &#123;<br>			System.out.println(i);<br>		&#125;<br>	&#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>
<p>两个方法有什么区别，Java之支持单继承，第二种方法避免了单继承的局限性。<br>继承Thread：线程代码存放在Thread子类run中<br>实现Runnable，线程代码在接口子类的run方法中。</p>
<p>多个线程在抢夺CPU的执行权<br>多线程的一个特性：随机性</p>
<p>为什么要覆盖run方法呢？<br>Thread类用于描述线程<br>该类就定义了一个功能，用于存储线程要运行的代码，该存储功能就是run方法<br>Thread类中的run方法用于存储线程要运行的代码。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs plain">Thread t &#x3D; new Thread();<br>t.start();<br></code></pre></td></tr></table></figure>
<p>线程资源同步例子<br>三个老师发80本作业</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs plain">	public static void main(String[] args) &#123;<br>		TeacherPackBook p &#x3D; new TeacherPackBook();<br>		new Thread(p,&quot;老师1&quot;).start();<br>		new Thread(p,&quot;老师2&quot;).start();<br>		new Thread(p,&quot;老师3&quot;).start();<br><br>	&#125;<br><br>&#125;<br><br>class TeacherPackBook implements Runnable&#123;<br>	private int book_num &#x3D; 80;<br>	@Override<br>	public void run() &#123;<br>		while(true)&#123;<br>			if(book_num&gt;0)&#123;<br>				Thread th &#x3D; Thread.currentThread();<br>				String th_name &#x3D; th.getName();<br>				System.out.println(&quot;正在发第&quot; + book_num-- + &quot;本作业&quot;);<br>			&#125;else &#123;<br>				break;<br>			&#125;<br>		&#125;<br>	&#125;<br>	<br>&#125;<br></code></pre></td></tr></table></figure>
<p>设置线程优先级：setPriority</p>
<p>线程的状态：</p>
<ol>
<li>被创建 new</li>
<li>运行 start()</li>
<li>冻结 sleep(time)  进入阻塞状态</li>
<li>等待wait() 进入等待状态，释放同步锁</li>
<li>唤醒 notify() </li>
<li>销毁stop()  或run方法结束。</li>
<li>临时状态 阻塞 具备运行资格，但没有执行权</li>
<li>冻结 放弃了执行资格</li>
<li>插队 jion</li>
<li>yield 线程进入可运行状态<br>关于线程状态：<br><a target="_blank" rel="noopener" href="https://www.cnblogs.com/hejing-swust/p/8038263.html">https://www.cnblogs.com/hejing-swust/p/8038263.html</a><br>使当前线程从执行状态（运行状态）变为可执行态（就绪状态）。cpu会从众多的可执行态里选择，也就是说，当前也就是刚刚的那个线程还是有可能会被再次执行到的，并不是说一定会执行其他线程而该线程在下一次中不会执行到了。</li>
</ol>
<p>Java线程中有一个Thread.yield( )方法，很多人翻译成线程让步。顾名思义，就是说当一个线程使用了这个方法之后，它就会把自己CPU执行的时间让掉，让自己或者其它的线程运行。<br>    - thread.join把指定的线程加入到当前线程，可以将两个交替执行的线程合并为顺序执行的线程。</p>
<p>比如在线程B中调用了线程A的Join()方法，直到线程A执行完毕后，才会继续执行线程</p>
<p>线程也有自己的名称：<br>Thread-编号<br>该编号从零开始。</p>
<p>static Thread currentThread() ：获取当前线程对象<br>getName()：获取线程名称。</p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://zhfhz.gitee.io/2021/Java%E5%AD%A6%E4%B9%A0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/" rel="tag">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag">技术</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2021/Java%E5%AD%A6%E4%B9%A0/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JS笔记
          
        </div>
      </a>
    
    
      <a href="/2020/%E8%B6%85%E6%98%9F%E5%B9%B3%E5%8F%B0XSS/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">超星平台XSS</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'QExPaN3yEdYGXDLijnPKdTc8-MdYXbMMI',
        app_key: 'kyQzz6yRJkdRHcMm0tpOvSYY',
        path: window.location.pathname,
        notify: 'true',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        ZHF
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src=''></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="岛"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="https://500px.me/ZHFHZ">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>




<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>

    
  </div>
</body>

</html>